package com.water.wl.newwater;

import android.app.Application;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

/**
 * Created by hp on 2016/1/12.
 * 如果使用volley，则在Application中使用单例进行 requestQueue的创建和获取
 * 记得在AndroidManifest中进行说明
 */
public class ApplicationController extends Application {

    private static final String TAG = ApplicationController.class.getSimpleName();
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;
    //private UploadManager uploadManager ;

    private static ApplicationController mInstance;

    private static boolean isLogin ;    //判断是否登录
    private static String userId;       //用户id,全局变量,后面调用全是调用的这个

    @Override
    public void onCreate(){
        super.onCreate();
        mInstance = this;
    }


    public static synchronized ApplicationController getInstance(){
        return mInstance;
    }


    public RequestQueue getRequestQueue(){
        if(requestQueue == null){
            requestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return requestQueue;
    }

    public ImageLoader getImageLoader(){
        getRequestQueue();
        if(imageLoader == null){
            imageLoader = new ImageLoader(requestQueue, new LruBitmapCache());
        }
        return imageLoader;
    }

   /* public UploadManager getUploadManager(){
        if(uploadManager == null){
            uploadManager = new UploadManager();
        }
        return uploadManager;
    }*/

    public <T> void addToRequestQueue(Request<T> req,String tag){
        req.setTag(TextUtils.isEmpty(tag)? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req){
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequest(Object tag){
        if(requestQueue != null){
            requestQueue.cancelAll(tag);
        }
    }

    public static boolean getIsLogin() {
        return isLogin;
    }

    public static void setIsLogin(boolean isLogin) {
        ApplicationController.isLogin = isLogin;
    }

    public static String getUserId() {
        return userId;
    }

    public static void setUserId(String userId) {
        ApplicationController.userId = userId;
    }
}
